package com.ch.clinking.Utils.NetUtils;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

public class OkHttpUtil {
    private static final OkHttpUtil instance = new OkHttpUtil();
    private final OkHttpClient okHttpClient;
    private final Handler handler = new Handler(Looper.getMainLooper());
    private final String BASE_URL = "http://8.146.200.102:8888";
    private final String IMG_URL = "http://8.146.200.102:8888/images/";
//    private final String BASE_URL = "http://192.168.3.154:8888";
//    private final String IMG_URL = "http://192.168.3.154:8888/images/";

    //private final String BASE_URL = "http://124.222.142.95:9090";
//    116.205.130.138
//    腾讯云：124.222.142.95
//    192.168.1.104
//    http://192.168.0.106:9090
    private OkHttpUtil(){
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();
    }
    public static OkHttpUtil getInstance() {
        return instance;
    }
    public String doGet(String url) throws IOException {
        Request request = new Request.Builder().url(BASE_URL + url).build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    public void doGetAsync(String url, DoCallBack doCallBack) {
        Request request = new Request.Builder().url(BASE_URL + url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                String string = null;
                try {
                    string = response.body().string();
                } catch (IOException e) {
                    handler.post(() -> doCallBack.onError(e));
                }
                String finalString = string;
                handler.post(() -> doCallBack.onSuccess(finalString));
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                handler.post(() -> doCallBack.onError(e));
            }
        });
    }

    public void doPostForm(String url, DoCallBack doCallBack, HashMap<String, String> hashMap) {
        FormBody.Builder formBody = new FormBody.Builder();
        if (hashMap != null) {
            for (String param : hashMap.keySet()) {
                formBody.add(param,hashMap.get(param));
            }
        }
        Request request = new Request.Builder().post(formBody.build()).url(BASE_URL + url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                String string = null;
                try {
                    string = response.body().string();
                } catch (IOException e) {
                    handler.post(() -> doCallBack.onError(e));
                }
                String finalString = string;
                handler.post(() -> doCallBack.onSuccess(finalString));
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                handler.post(() -> doCallBack.onError(e));
            }
        });
    }

    public void doPostJson(String url, DoCallBack doCallBack, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        Request request = new Request.Builder().post(requestBody).url(BASE_URL + url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                String string = null;
                try {
                    string = response.body().string();
                } catch (IOException e) {
                    handler.post(() -> doCallBack.onError(e));
                }
                String finalString = string;
                handler.post(() -> doCallBack.onSuccess(finalString));
            }

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                handler.post(() -> doCallBack.onError(e));
            }
        });
    }

    public String getBASE_URL() {
        return BASE_URL;
    }

    public String getIMG_URL() {
        return IMG_URL;
    }
}
